<?php require_once('common.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pantry extends Common {
	function __construct() {
		parent::__construct("pantry");
		$this->load->model(array('bahan_baku'));
	}
	
	public function index(){
		$data['bahan'] = $this->bahan_baku->get()->result_array();
		$data['pesan'] = $this->session->flashdata('pesan');
		$this->load->view('pantry/list_bahan', $data);
	}
	
	function add_bahan_baku(){
		$data['mode'] = 'ADD';
		$this->load->view('pantry/form_bahan',$data);
	}

	function edit_bahan_baku($id){
		$data['mode'] = 'EDIT';
		$data['detail'] = $this->bahan_baku->get(array('id_bahan_baku' => $id))->row_array();
		$this->load->view('pantry/form_bahan',$data);	
	}
	
	function restok_bahan_baku($id){
		$data['detail'] = $this->bahan_baku->get(array('id_bahan_baku' => $id))->row_array();
		$this->load->view('pantry/form_restok_bahan',$data);	
	}
	
	function save_bahan_baku(){
		$this->layout = FALSE;
		$mode = $this->input->post('mode');
		$data = array('nama' 		=> $this->input->post('nama'),
					  'stok'	=> $this->input->post('stok'),
					  'id_petugas' => $this->user_data['id_petugas']);
		
		if($mode == 'ADD'){
			$id = $this->bahan_baku->add($data);
			$success = $id;
			//add transaksi gudang
			$data_transaksi = array('id_petugas' => $this->user_data['id_petugas'],
									'id_bahan_baku' => $id,
									'tgl_expired' => $this->input->post('tgl_expired'),
									'jumlah' => $this->input->post('stok'));
			$this->bahan_baku->add_gudang($data_transaksi);
		}else{
			$id = $this->input->post('id_bahan_baku');
			$success = $this->bahan_baku->edit($id, $data);
		}
		
		if($success){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Bahan Baku berhasil disimpan."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Bahan Baku gagal disimpan."));
		}
		redirect(base_url()."index.php/pantry");
	}
	
	function save_restok_bahan(){
		$id = $this->input->post('id_bahan_baku');
		$stok = $this->input->post('stok');
		$data_transaksi = array('id_petugas' => $this->user_data['id_petugas'],
								'id_bahan_baku' => $id,
								'tgl_expired' => $this->input->post('tgl_expired'),
								'jumlah' => $this->input->post('jumlah'));
		if($this->bahan_baku->add_gudang($data_transaksi)){
			$success = $this->bahan_baku->edit($id, array('stok' => ($stok + $data_transaksi['jumlah'])));
		}else{
			$success = false;
		}	
		
		if($success){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Bahan Baku berhasil direstok."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Bahan Baku gagal direstok."));
		}
		redirect(base_url()."index.php/pantry");
	}
	
	function delete_bahan_baku($id){
		if($this->bahan_baku->delete($id)){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Bahan Baku berhasil dihapus."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Bahan Baku gagal dihapus."));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function pantau_kadaluarsa(){
		$data['bahan'] = $this->bahan_baku->get_gudang();
		$this->load->view('pantry/pantau_kadaluarsa', $data);
	}
	
}